home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Mousebroken 1.0.1 / source / Mousebroken source ƒ / init code / meat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  7.5 KB  |  263 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        meat.c
  4.  
  5. Purpose:    This module handles figuring out which module to open,
  6.             and then opening it.  Also installing the actual VBL.
  7.             
  8.  
  9. Mousebroken -=- your computer isn't truly broken until it's mousebroken
  10. Copyright (C) 1993 Mark Pilgrim
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program in a file named "GNU General Public License".
  24. If not, write to the Free Software Foundation, 675 Mass Ave,
  25. Cambridge, MA 02139, USA.
  26.  
  27. \**********************************************************************/
  28.  
  29. #include "structs.h"
  30. #include "meat.h"
  31. #include "prefs.h"
  32. #include "Retrace.h"
  33. #include "Folders.h"
  34.  
  35. #define MODULE_TYPE        'MMdl'
  36. #define MODULE_CREATOR    'MBrk'
  37. #define MODULE_FOLDER    "\pMouse Modules"
  38.  
  39. int InstallTheMouseVBL(PrefHandle cdevStorage)
  40. {
  41.     VBLTask            *vblPtr;
  42.     Handle            ourCode;
  43.     int                oldResFile;
  44.     
  45.     oldResFile=CurResFile();
  46.     UseResFile((**cdevStorage).moduleRefNum);
  47.     ourCode=0L;
  48.     ourCode=GetResource('vbl ', 668);
  49.     UseResFile(oldResFile);
  50.     if (ourCode!=0L)
  51.     {
  52.         if (*ourCode==0L)
  53.             LoadResource(ourCode);
  54.         if (*ourCode==0L)
  55.             return kCantGetResource;
  56.         
  57.         HLock(ourCode);
  58.         DetachResource(ourCode);
  59.         
  60.         vblPtr = (VBLTask*) NewPtrSys(sizeof(VBLTask));
  61.         vblPtr->qType = vType;
  62.         vblPtr->vblAddr = (ProcPtr) *ourCode;
  63.         vblPtr->vblCount = 1;
  64.         vblPtr->vblPhase = 0;
  65.         VInstall(vblPtr);
  66.         (**cdevStorage).mouseCodePtr=(unsigned long)ourCode;
  67.         (**cdevStorage).mouseVBLPtr=(unsigned long)vblPtr;
  68.     }
  69.     else return kCantGetResource;
  70.  
  71.     return prefs_allsWell;
  72. }
  73.  
  74. int GetModuleInfo(PrefHandle cdevStorage)
  75. {
  76.     OSErr            isHuman;
  77.     int                vRefNum;
  78.     long            dirID;
  79.     CInfoPBRec        pb_dir;
  80.     
  81.     if ((**cdevStorage).moduleIndex==0)
  82.         return GetModuleInfoFromIndex(cdevStorage, 1);
  83.     
  84.     isHuman=FindFolder(kOnSystemDisk, kSystemFolderType, kDontCreateFolder, &vRefNum,
  85.                 &dirID);                    /* find system folder */
  86.     if (isHuman!=noErr)
  87.         return kCantFindSystemFolder;
  88.     
  89.     pb_dir.dirInfo.ioCompletion=0L;
  90.     pb_dir.dirInfo.ioNamePtr=MODULE_FOLDER;
  91.     pb_dir.dirInfo.ioVRefNum=vRefNum;
  92.     pb_dir.dirInfo.ioFDirIndex=0;             /* very important */
  93.     pb_dir.dirInfo.ioDrDirID=dirID;
  94.     isHuman=PBGetCatInfo(&pb_dir, FALSE);    /* get info on "Mouse Modules" dir */
  95.     if (isHuman!=noErr)
  96.         return kCantOpenModuleFolder;
  97.  
  98.     if (!AtLeastOneModule(vRefNum, pb_dir.dirInfo.ioDrDirID))
  99.         return kNoModules;                    /* check if there are any modules at all */
  100.     
  101.     return FindCurrentModule(cdevStorage, vRefNum, pb_dir.dirInfo.ioDrDirID,
  102.             pb_dir.dirInfo.ioDrNmFls);
  103. }
  104.  
  105. int GetModuleInfoFromIndex(PrefHandle cdevStorage, int direction)
  106. {
  107.     OSErr            isHuman;
  108.     int                vRefNum;
  109.     long            dirID;
  110.     CInfoPBRec        pb_dir;
  111.     HParamBlockRec    pb_basic;
  112.     Str63            theName;
  113.     int                i;
  114.     int                tempIndex;
  115.     int                resultCode;
  116.     
  117.     isHuman=FindFolder(kOnSystemDisk, kSystemFolderType, kDontCreateFolder, &vRefNum,
  118.                 &dirID);                    /* find system folder */
  119.     if (isHuman!=noErr)
  120.         return kCantFindSystemFolder;
  121.     
  122.     pb_dir.dirInfo.ioCompletion=0L;
  123.     pb_dir.dirInfo.ioNamePtr=MODULE_FOLDER;
  124.     pb_dir.dirInfo.ioVRefNum=vRefNum;
  125.     pb_dir.dirInfo.ioFDirIndex=0; /* very important */
  126.     pb_dir.dirInfo.ioDrDirID=dirID;
  127.     isHuman=PBGetCatInfo(&pb_dir, FALSE);    /* get info on "Mouse Modules" dir */
  128.     if (isHuman!=noErr)
  129.         return kCantOpenModuleFolder;
  130.  
  131.     if (!AtLeastOneModule(vRefNum, pb_dir.dirInfo.ioDrDirID))
  132.         return kNoModules;                    /* check if there are any modules at all */
  133.     
  134.     if ((**cdevStorage).moduleIndex>0)
  135.     {
  136.         resultCode=FindCurrentModule(cdevStorage, vRefNum, pb_dir.dirInfo.ioDrDirID,
  137.                     pb_dir.dirInfo.ioDrNmFls);    /* get index # of current module */
  138.         if (resultCode!=allsWell)
  139.             return resultCode;
  140.     }
  141.     
  142.     tempIndex=(**cdevStorage).moduleIndex;
  143.     do
  144.     {
  145.         tempIndex+=direction;
  146.         if (tempIndex==0)
  147.             tempIndex=pb_dir.dirInfo.ioDrNmFls;
  148.         else if (tempIndex>pb_dir.dirInfo.ioDrNmFls)
  149.             tempIndex=1;
  150.         
  151.         pb_basic.fileParam.ioCompletion=0L;
  152.         pb_basic.fileParam.ioNamePtr=theName;
  153.         pb_basic.fileParam.ioVRefNum=vRefNum;
  154.         pb_basic.fileParam.ioFDirIndex=tempIndex;
  155.         pb_basic.fileParam.ioDirID=pb_dir.dirInfo.ioDrDirID;
  156.         isHuman=PBGetCatInfo(&pb_basic, FALSE);    /* get info on previous/next module */
  157.         if (isHuman!=noErr)
  158.             return kCantOpenModule;
  159.     }
  160.     while ((pb_basic.fileParam.ioFlFndrInfo.fdType!=MODULE_TYPE) ||
  161.             (pb_basic.fileParam.ioFlFndrInfo.fdCreator!=MODULE_CREATOR));
  162.     
  163.     (**cdevStorage).moduleIndex=tempIndex;
  164.     (**cdevStorage).moduleFS.vRefNum=vRefNum;
  165.     (**cdevStorage).moduleFS.parID=pb_dir.dirInfo.ioDrDirID;
  166.         for (i=theName[0]; i>=0; i--)
  167.         (**cdevStorage).moduleFS.name[i]=theName[i];
  168.     return allsWell;
  169. }
  170.  
  171. Boolean AtLeastOneModule(int vRefNum, long dirID)
  172. {
  173.     HParamBlockRec        pb_basic;
  174.     int                    dirIter;
  175.     Boolean                foundModule;
  176.     Str63                theName;
  177.     OSErr                isHuman;
  178.     
  179.     dirIter=0;
  180.     do
  181.     {
  182.         dirIter++;
  183.         pb_basic.fileParam.ioCompletion=0L;
  184.         pb_basic.fileParam.ioNamePtr=theName;
  185.         pb_basic.fileParam.ioVRefNum=vRefNum;
  186.         pb_basic.fileParam.ioFDirIndex=dirIter;
  187.         pb_basic.fileParam.ioDirID=dirID;
  188.         isHuman=PBGetCatInfo(&pb_basic, FALSE);
  189.         if (isHuman!=noErr)
  190.             return FALSE;
  191.         foundModule=(pb_basic.fileParam.ioFlFndrInfo.fdType==MODULE_TYPE) &&
  192.             (pb_basic.fileParam.ioFlFndrInfo.fdCreator==MODULE_CREATOR);
  193.     }
  194.     while ((isHuman==noErr) && (!foundModule));
  195.     
  196.     return (isHuman==noErr);
  197. }
  198.  
  199. int FindCurrentModule(PrefHandle cdevStorage, int vRefNum, long dirID, int numFiles)
  200. {
  201.     int                tempIndex;
  202.     Boolean            isCorrectModule;
  203.     HParamBlockRec    pb_basic;
  204.     Str63            theName;
  205.     OSErr            isHuman;
  206.     int                i;
  207.     
  208.     isCorrectModule=FALSE;
  209.     for (tempIndex=1; (tempIndex<=numFiles) && (!isCorrectModule); tempIndex++)
  210.     {
  211.         pb_basic.fileParam.ioCompletion=0L;
  212.         pb_basic.fileParam.ioNamePtr=theName;
  213.         pb_basic.fileParam.ioVRefNum=vRefNum;
  214.         pb_basic.fileParam.ioFDirIndex=tempIndex;
  215.         pb_basic.fileParam.ioDirID=dirID;
  216.         isHuman=PBGetCatInfo(&pb_basic, FALSE);    /* get info on current module */
  217.         if (isHuman!=noErr)
  218.             return kCantOpenModule;
  219.         isCorrectModule=(pb_basic.fileParam.ioFlFndrInfo.fdType==MODULE_TYPE) &&
  220.             (pb_basic.fileParam.ioFlFndrInfo.fdCreator==MODULE_CREATOR);
  221.         for (i=(**cdevStorage).moduleFS.name[0]; (i>=0) && (isCorrectModule); i--)
  222.             isCorrectModule=((**cdevStorage).moduleFS.name[i]==theName[i]);
  223.     }
  224.     
  225.     if (!isCorrectModule)
  226.     {
  227.         (**cdevStorage).moduleIndex=0;
  228.         return GetModuleInfoFromIndex(cdevStorage, 1);
  229.     }
  230.  
  231.     (**cdevStorage).moduleIndex=tempIndex-1;
  232.     (**cdevStorage).moduleFS.vRefNum=vRefNum;
  233.     (**cdevStorage).moduleFS.parID=dirID;
  234.     
  235.     return allsWell;
  236. }
  237.  
  238. int OpenTheModule(PrefHandle cdevStorage)
  239. {
  240.     unsigned long        oldTopMapHndl;
  241.     OSErr                theResError;
  242.     
  243.     (**cdevStorage).oldRefNum=CurResFile();
  244.     oldTopMapHndl=(unsigned long)TopMapHndl;
  245.     (**cdevStorage).moduleRefNum=HOpenResFile((**cdevStorage).moduleFS.vRefNum,
  246.         (**cdevStorage).moduleFS.parID, (**cdevStorage).moduleFS.name, fsRdPerm);
  247.     theResError=ResError();
  248.     (**cdevStorage).moduleAlreadyOpen=(oldTopMapHndl==(unsigned long)TopMapHndl);
  249.     if (theResError==noErr)
  250.     {
  251.         UseResFile((**cdevStorage).moduleFS.parID);
  252.     }
  253.     
  254.     return (theResError==noErr) ? allsWell : kCantOpenModule;
  255. }
  256.  
  257. int CloseTheModule(PrefHandle cdevStorage)
  258. {
  259.     if (!((**cdevStorage).moduleAlreadyOpen))
  260.         CloseResFile((**cdevStorage).moduleRefNum);
  261.     UseResFile((**cdevStorage).oldRefNum);
  262. }
  263.